import { defineStore } from "pinia";
import { ref } from "vue";

export const useTabsStore = defineStore("tabs", () => {
    const tabs = ref([{ name: "数据看板", url: "/dashboard", icon: "DataLine" }]);
    const currentTab = ref({ name: "数据看板", url: "/dashboard" }); // 或者将 url 改为 path，但要保持一致性

    const addTab = (name, url, icon) => {
        if (!tabs.value.some(tab => tab.url === url)) {
            tabs.value.push({ name, url, icon });
        }
    };

    const setCurrentTab = (name, path) => {
        currentTab.value = { name, path }
    }
    const removeTab = (name) => {
        //如果删除的是高亮的
        if (currentTab.value.name === name){
            const currentIndex = tabs.value.findIndex(tab => tab.name === name);
             // 如果被移除的标签是当前标签，更新当前标签为前面一个标签
             if(currentIndex!=0){
                currentTab.value = tabs.value[currentIndex - 1]; 
             }else{
                return
             }   
        }

        // 删除指定路径的选项卡
        tabs.value = tabs.value.filter(tab => tab.name !== name)
    }
    return { tabs, addTab, currentTab, setCurrentTab,removeTab };
});

